package com.tanhua.manager.listeners;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.db.Log;
import com.tanhua.manager.service.LogService;
import org.joda.time.DateTime;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class LogLinstener {

    @Autowired
    private LogService logService;

//    1、声明队列 2、声明交换机 3、绑定队列和交换机
//            rabbitTemplate.convertAndSend("log.exchange","user.log",jsonString);

    @RabbitListener(bindings=@QueueBinding(value = @Queue(name="log.queue"),exchange = @Exchange(name = "log.exchange",type = ExchangeTypes.TOPIC),key = "#.log"))
    public void saveLog(String mapString){
        Map map = JSON.parseObject(mapString, Map.class);
//        mqMap.put("userId",user.getId());
//        mqMap.put("type",isNew?"0102":"0101");
//        mqMap.put("logTime", DateTime.now().toString("yyyy-MM-dd"));
        long userId = Long.parseLong(map.get("userId").toString());
        String type = (String) map.get("type");
        String logTime = (String) map.get("logTime");


        Log log = new Log();
        log.setUserId(userId);
        log.setLogTime(logTime);
        log.setPlace("北京顺义"); //
        log.setEquipment("华为p40 pro");
        log.setType(type);

        logService.save(log);
        System.out.println("执行了MQ的消费者代码。实现了log新增。。。。。。。。。。。。。。。。。。");


    }



}
